package com.wjh.kt_vod.controller;

import com.wjh.kt_vod.exception.GgkException;
import com.wjh.kt_vod.result.Result;
import com.wjh.kt_vod.service.VodService;
import com.wjh.kt_vod.utils.ConstantPropertiesUtil;
import com.wjh.kt_vod.utils.Signature;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.Random;

/**
 * Wang_Junhong
 * 2022/7/15
 * 文件描述：腾讯云点播
 */

@Api(tags = "腾讯云点播")
@RestController
@RequestMapping("/admin/vod")
public class VodController {

    @Autowired
    private VodService vodService;

    @GetMapping("/user/sign")
    public Result sign(){
        Signature sign = new Signature();
        // 设置 App 的云 API 密钥
        sign.setSecretId(ConstantPropertiesUtil.ACCESS_KEY_ID);
        sign.setSecretKey(ConstantPropertiesUtil.ACCESS_KEY_SECRET);
        sign.setCurrentTime(System.currentTimeMillis() / 1000);
        sign.setRandom(new Random().nextInt(java.lang.Integer.MAX_VALUE));
        sign.setSignValidDuration(3600 * 24 * 2); // 签名有效期：2天
        try {
            String signature = sign.getUploadSignature();
            return Result.ok(signature);
        } catch (Exception e) {
            throw new GgkException(20001,"获取签名失败");
        }

    }

    /**
     *
     * @param multipartFile
     * @return
     */
    @ApiOperation("腾讯云点播")
    @PostMapping("/upload")
    public Result uplod(
            @RequestParam("file") @ApiParam(name = "file",value = "视频文件",required = true)MultipartFile multipartFile ) {
        String fieldId = vodService.uploadVideo(multipartFile);
        return Result.ok(fieldId);
    }

    @ApiOperation("删除腾讯云视频")
    @DeleteMapping("/remove/{fileId}")
    public Result remove(@PathVariable("fileId") String fileId) {
        vodService.removeById(fileId);
        return Result.ok();
    }

}
